<template>
  <div class="register-container">
    <div class="register-box">
      <h2>注册</h2>
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="formData.username"
            required
            placeholder="请输入用户名"
          />
        </div>
        <div class="form-group">
          <label for="realName">真实姓名</label>
          <input
            type="text"
            id="realName"
            v-model="formData.realName"
            required
            placeholder="请输入真实姓名"
          />
        </div>
        <div class="form-group">
          <label for="phone">手机号码</label>
          <input
            type="tel"
            id="phone"
            v-model="formData.phone"
            required
            pattern="^1[3-9]\d{9}$"
            placeholder="请输入手机号码"
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            required
            placeholder="请输入密码"
          />
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="confirmPassword"
            required
            placeholder="请再次输入密码"
          />
        </div>
        <div class="form-group">
          <label for="userType">用户类型</label>
          <select id="userType" v-model="formData.userType" required>
            <option value="0">普通客户</option>
            <option value="1">员工</option>
          </select>
        </div>
        <div class="form-group" v-if="formData.userType === '0'">
          <label for="company">公司名称</label>
          <input
            type="text"
            id="company"
            v-model="formData.company"
            required
            placeholder="请输入公司名称"
          />
        </div>
        <div class="form-actions">
          <button type="submit" :disabled="loading">
            {{ loading ? '注册中...' : '注册' }}
          </button>
          <router-link to="/login" class="login-link">返回登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { ElMessage } from 'element-plus';

const router = useRouter();
const userStore = useUserStore();

const formData = ref({
  username: '',
  realName: '',
  password: '',
  phone: '',
  userType: '0',
  company: '',
});

const confirmPassword = ref('');
const loading = ref(false);

// 监听userType变化
watch(() => formData.value.userType, (newType) => {
  if (newType === '1') {
    formData.value.company = '本公司';
  } else {
    formData.value.company = '';
  }
});

const validateForm = () => {
  if (formData.value.password !== confirmPassword.value) {
    alert('两次输入的密码不一致');
    return false;
  }

  // 验证手机号码格式
  const phoneRegex = /^1[3-9]\d{9}$/;
  if (!phoneRegex.test(formData.value.phone)) {
    alert('请输入正确的手机号码格式');
    return false;
  }

  return true;
};

const handleSubmit = async () => {
  if (!validateForm()) {
    return;
  }

  loading.value = true;
  try {
    const result = await userStore.registerUser(formData.value);
    if (result.success) {
      ElMessage.success('注册成功，请等待审核');
      router.push('/login');
    } else {
      ElMessage.error(result.data || result.message || '注册失败');
    }
  } catch (error) {
    ElMessage.error('注册失败：' + error.message);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  /* background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); */
  background: linear-gradient(135deg, #b92b27 0%, #1565c0 100%);
  padding: 1rem;
}

.register-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 2.5rem;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 480px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

h2 {
  text-align: center;
  margin-bottom: 2rem;
  color: #2c3e50;
  font-size: 1.8rem;
  font-weight: 600;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #2c3e50;
  font-weight: 500;
  font-size: 0.95rem;
}

input, select {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  transition: border-color 0.2s ease;
}

input:focus, select:focus {
  outline: none;
  border-color: #667eea;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

button {
  width: 100%;
  height: 44px;
  font-size: 1rem;
  font-weight: 500;
  border-radius: 8px;
  /* background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); */
  background: linear-gradient(135deg, #b92b27 0%, #1565c0 100%);
  color: white;
  border: none;
  cursor: pointer;
  transition: transform 0.2s ease;
}

button:hover {
  transform: translateY(-1px);
}

button:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
}

.login-link {
  text-align: center;
  color: #764ba2;
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.2s ease;
}

.login-link:hover {
  color: #b92b27;
}

@media screen and (max-width: 480px) {
  .register-box {
    padding: 2rem;
  }

  h2 {
    font-size: 1.5rem;
  }
}
</style> 